package day22.HomeWork;


import java.util.Random;
import java.util.Scanner;

public class Game {
    static int count = 1;
    Player player;

    //    Level levels[] = LevelParam.levels;
//    Integer levelNo = player.getLevelNo();
    public Game(Player player) {
        this.player = player;
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    public String printStr() {//Game类中的player属性，代表玩家，查询player的级别号，然后根据级别号到LevelParam类中获取该级别的字符串长度
        //字符串长度固定可以通过for循环来实现，而随机内容可以通过获取随机数，而不同随机数对应不同字符来实现
        StringBuffer buffer = new StringBuffer();
        Random random = new Random();

//        Level levels[] = LevelParam.levels;
        int strLength = LevelParam.levels[count- 1].getStrLength();

        for (int i = 0; i < strLength; i++) {
            // 1.1、产生随机数
            int rand = random.nextInt(strLength);
            // 1.2、根据随机数拼接字符串
            switch (rand) {
                case 0:
                    buffer.append(">");
                    break;
                case 3:
                    buffer.append(">");
                    break;
                case 1:
                    buffer.append("<");
                    break;
                case 2:
                    buffer.append("*");
                    break;
                case 4:
                    buffer.append("<");
                    break;
                case 5:
                    buffer.append("*");
                    break;
                default:
                    buffer.append("*");
            }

        }
        return buffer.toString();
    }//输出字符串，作为游戏

    public void printResult(String s) {
        Scanner sc = new Scanner(System.in);
        player.setStartTime(System.currentTimeMillis());

        //开始打字时间
        String str = sc.next();

        long currentTime = System.currentTimeMillis();
// 如果超时
        player.setElapasedTime((int) ((currentTime - player.getStartTime()) / 1000));
        if ((currentTime - player.getStartTime()) / 1000 > LevelParam.levels[count - 1].getTimeLimt()) {
            System.out.println("你输入太慢了，已经超时，退出！");
            System.exit(1);
        }
        if (!str.equals(s)) {
            System.out.println("输入错误，退出!");
            System.exit(1);
        }
// 计算玩家当前积分
        player.setCurScore(player.getCurScore()
                + LevelParam.levels[count - 1].getPerScore());
        System.out.println("输入正确，您的积分为" + player.getCurScore() + "，您的级别为" + count + ",已用时间" + player.getElapasedTime() + "秒。");
        int strTimes = LevelParam.levels[count - 1].getStrTimes();
        System.out.println(strTimes);
        int score=LevelParam.levels[count-1].getPerScore();
        System.out.println(score);

        if (count < 6) {
            if(player.getCurScore()>=(strTimes*score)){
                player.setCurScore(0);
                count++;
            }
        } else {
            System.out.println("闯关成功");
            System.exit(1);
        }

    }
}

class Player {
    private Integer levelNo;//玩家级别
    private Integer curScore;//当前积分
    private Long startTime;//当前级别开始时间
    private Integer elapasedTime;//当前级别已用时间

    public void paly() {
        System.out.println("paly初始化");
    }

    public Player(Integer levelNo) {
        this.levelNo = levelNo;
        this.curScore = 0;
        this.startTime = 0L;
        this.elapasedTime = 10;
    }

    public Integer getLevelNo() {
        return levelNo;
    }

    public void setLevelNo(Integer levelNo) {
        this.levelNo = levelNo;
    }

    public Integer getCurScore() {
        return curScore;
    }

    public void setCurScore(Integer curScore) {
        this.curScore = curScore;
    }

    public Long getStartTime() {
        return startTime;
    }

    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }

    public Integer getElapasedTime() {
        return elapasedTime;
    }

    public void setElapasedTime(Integer elapasedTime) {
        this.elapasedTime = elapasedTime;
    }
}

class Level {
    private Integer levelno;//各级级别号
    private Integer strLength;//级别一次输出字符串的长度
    private Integer strTimes;//输出字符串的次数
    private Integer timeLimt;//闯关时间限制
    private Integer perScore;//正确输入一次的得分

    public Level(Integer levelno, Integer strLength, Integer strTimes, Integer timeLimt, Integer perScore) {
        this.levelno = levelno;
        this.strLength = strLength;
        this.strTimes = strTimes;
        this.timeLimt = timeLimt;
        this.perScore = perScore;
    }

    public Integer getLevelno() {
        return levelno;
    }

    public void setLevelno(Integer levelno) {
        this.levelno = levelno;
    }

    public Integer getStrLength() {
        return strLength;
    }

    public void setStrLength(Integer strLength) {
        this.strLength = strLength;
    }

    public Integer getStrTimes() {
        return strTimes;
    }

    public void setStrTimes(Integer strTimes) {
        this.strTimes = strTimes;
    }

    public long getTimeLimt() {
        return timeLimt;
    }

    public void setTimeLimt(Integer timeLimt) {
        this.timeLimt = timeLimt;
    }

    public Integer getPerScore() {
        return perScore;
    }

    public void setPerScore(Integer perScore) {
        this.perScore = perScore;
    }


}

class LevelParam {
    public final static Level levels[] = new Level[6];

    static {
        levels[0] = new Level(1, 2, 10, 30, 1);
        levels[1] = new Level(2, 3, 9, 26, 2);
        levels[2] = new Level(3, 4, 8, 22, 5);
        levels[3] = new Level(4, 5, 7, 18, 8);
        levels[4] = new Level(5, 6, 6, 15, 10);
        levels[5] = new Level(6, 7, 5, 12, 15);
    }

}

class StaticCodeBlock {
    static String name = "defname";

    static {
        name = "staticname";
        System.out.println("execute static code block");
    }

    public StaticCodeBlock() {
        System.out.println("execute constructor");
    }
}

class TestStaticCodeBlock {
    static {
        System.out.println("execute static code block in Test");
    }

    public static void main(String[] args) {
        System.out.println("游戏开始");
        Game game = new Game(new Player(1));
        while (true) {
            String s = game.printStr();
            System.out.println(s);
            game.printResult(s);
        }


    }
}